<?php
/**
 * Created by PhpStorm.
 * User: aako
 * Date: 15-12-8
 * Time: 下午5:41
 */

namespace app\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\categoryBrand\CategoryBrandRequest;
use App\Http\Requests\Admin\categoryBrand\AddCategoryBrandRequest;

use App\Models\CategoryBrand;
use App\Models\Category;
use App\Models\Brand;
use Gate;
use App\Exceptions\Admin\PermissionDeniedException;
use Illuminate\Support\Facades\Input;
use App\Commands\OperationLogCommand;

class CategoryBrandController extends Controller
{
    public function category_brand_gate()
    {
        if (!Gate::allows('goods','')) {
            throw new PermissionDeniedException();
        }//end if()

    }//end func __construce()

    public function index(Request $request)
    {
    }


    /**
     * 向分类下添加品牌
     *
     * @param  int  $cat_id
     * @return \Illuminate\Http\Response
     */
    public function create($cat_id)
    {
        $this->category_brand_gate();

        $brand = Brand::all();
        $brandArr = [];
        foreach($brand as $key=>$value){
            $brandArr[$value->brand_id] = $value->brand_name;
        }//end foreach()

        return view('admin.category.addBrand',compact('cat_id', 'brand', 'brandArr'));
    }

    public function store(AddCategoryBrandRequest $addCategoryBrandRequest)
    {
        $this->category_brand_gate();

        $cat_id = $addCategoryBrandRequest->cat_id;
        $brand_id = $addCategoryBrandRequest->brand_id;

        $count = CategoryBrand::where('cat_id', $cat_id)->where('brand_id', $brand_id)->count();
        if($count==0){
            CategoryBrand::insert(['cat_id'=>$cat_id, 'brand_id'=>$brand_id]);
        }//end if()

        return Redirect('admin/categoryBrand/categoryBrands/'.$cat_id);
    }//end func store()

    /**
     * 根据分类id查询分类下的品牌
     *
     * @param  int  $cat_id
     * @return \Illuminate\Http\Response
     */
    public function show($cat_id, CategoryBrandRequest $categoryBrandRequest)
    {
        $this->category_brand_gate();

        $categoryBrand = CategoryBrand::where('cat_id', $cat_id)->get();

        $brandArr = [];
        foreach($categoryBrand as $key=>$value){
            $brand_id = $value->brand_id;

            $brand = Brand::find($brand_id);

            $brand_name = $brand->brand_name;
            $brandArr[$brand_id] = $brand_name;

        }//end foreach()

        return Response()->json($brandArr);
    }

    public function edit($id)
    {

    }

    public function update($id)
    {
    }

    public function destroy($id)
    {

    }

    public function categoryBrand($cat_id, $brand_id){
        $this->category_brand_gate();

        $categorBrand = CategoryBrand::insertGetId(['cat_id'=>$cat_id, 'brand_id'=>$brand_id]);

        return $categorBrand;
    }//end func categoryBrand()

    /**
     * 删除分类下的品牌
     *
     * @param  int  $cat_id：分类id
     * @param  int  $brand_id：品牌id
     * @return \Illuminate\Http\Response
     */
    public function delCategoryBrand($cat_id, $brand_id){

        $this->category_brand_gate();

        CategoryBrand::where('cat_id',$cat_id)->where( 'brand_id', $brand_id)->delete();

        return Redirect('admin/categoryBrand/categoryBrands/'.$cat_id);
    }//end func categoryBrand()


    /**
     * 根据分类id查询分类下的品牌
     *
     * @param  int  $cat_id：分类id
     * @return \Illuminate\Http\Response
     */
    public function categoryBrands($cat_id){

        $this->category_brand_gate();

//        $categoryBrands = CategoryBrand::where('cat_id', $cat_id)
//            ->leftjoin('brand', 'category_brand.brand_id', '=', 'brand.brand_id')
//            ->paginate(10);select
        $categoryBrands = CategoryBrand::select('category_brand.*','brand.brand_name','brand.brand_desc')
            ->where('category_brand.cat_id', $cat_id)
            ->leftjoin('brand', 'category_brand.brand_id', '=', 'brand.brand_id')
            ->paginate(10);
        //dd($categoryBrands);

        return view('admin.category.brand',compact(['cat_id', 'categoryBrands']));
    }//end func categoryBrands()

    public function toggleShow(){

        $this->category_brand_gate();

        $id = Input::get("id");
        $is_recommend = CategoryBrand::where("id",$id)->pluck("is_recommend");
       // dd($is_recommend);
        $brand_id = CategoryBrand::where("id",$id)->pluck("brand_id");
        $cat_id = CategoryBrand::where("id",$id)->pluck("cat_id");
        //dd($brand_id);
        $data = array();

        if($is_recommend==0){
            $data['is_recommend'] = 1;
            $data['id'] = $id;
            $res = CategoryBrand::where("id",$id)->update(['is_recommend'=>1]);
            $is_recommend_arr = CategoryBrand::where("brand_id",$brand_id)->where("is_recommend",0)->get()->toArray();
            if($is_recommend_arr){
                $brandres = true;
            }else{
                $brandres = Brand::where("brand_id",$brand_id)->update(['is_recommend'=>1]);
            }
        }else{
            $data['is_recommend'] = 0;
            $data['id'] = $id;
            $res = CategoryBrand::where("id",$id)->update(['is_recommend'=>0]);
            if(Brand::where("brand_id",$brand_id)->pluck('is_recommend')==1){
                $brandres = Brand::where("brand_id",$brand_id)->update(['is_recommend'=>0]);
                //dd($brandres);
            }else{
                $brandres = true;
            }
            //dd("brandres".$brandres);
        }
        if($res != false && $brandres != false){
            $data['info'] = true;
        }else{
            $data['info'] = false;
        }
        return $data;

    }

}